#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-

import web
import os
from web.contrib.template import render_mako
import actions  
import sys
import setting

class index :
    def GET(s):
        web.header('Content-type', 'text/html')
        return app.render.index()

class static :
    def GET(s, subp, filename):
        return open('%s/%s/%s' 
                % (app.templates_path, subp, filename)).read();

class js_handler :
    def GET (s, filename):
        web.header('Content-Type', 'text/javascript')
        return open('%s/js/%s' 
            % (app.templates_path, filename), 'r').read();

        
class css_handler :
    def GET (s, filename):
        web.header('Content-Type', 'text/css')
        return open('%s/css/%s' 
                % (app.templates_path, filename), 'r').read();



class images :
    def GET(s, filename):
        ext = filename.split('.')[-1]
        mimetype = {
            'png':'images/png',
            'jpg':'images/jpeg',
            'gif':'images/gif',
            'ico':'images/x-icon',
        }
        web.header('Content-Type', mimetype[ext])
        return open('%s/imgs/%s' 
                % (app.templates_path, filename), 'rb').read();

class ubinote_app:
    def __init__(s):
        s.urls = (
            '/', 'index',
            '/imgs/(.*)', 'images',
            '/css/(.*)',  'css_handler',
            '/js/(.*)',   'js_handler',

            '/act/sayhello/(.*)',       actions.sayhello,

            '/notes/',                  actions.list_note,
            '/note/add/(.*)',           actions.add_note, 
            '/note/(\d*)/remove',       actions.remove_note, 
            '/note/(\d*)/rename/(.*)',  actions.rename_note,
            '/note/(\d*)',              actions.get_note, 
            
            '/note/(\d*)/blocks/',      actions.list_block,
            '/note/(\d*)/add-block',    actions.add_block,

            '/tags/(\d*)',              actions.list_tag_by_block,
            '/search/(.*)',             actions.do_search,

            '/blocks/save-pos',         actions.save_blocks_pos,
            '/blocks/save-state',       actions.save_blocks_state,
            '/block/(\d*)/save-tags',        actions.save_block_tags,
            '/block/(\d*)/save-content',     actions.save_block_content,
            '/block/(\d*)/save-state/(\d*)',       actions.save_block_state,
            '/block/(\d*)/remove',      actions.remove_block,
            )


        s.app = web.application(s.urls, globals())
        s.templates_path = 'templates'
        s.render = render_mako(
                directories=[s.templates_path], 
                input_encoding='utf-8',
                output_encoding='utf-8',
                )
        user_home = os.path.expanduser('~')
        s.setting_dir = '%s/.config/ubinote' % user_home
        s.cfg_url = '%s/ubinote.conf' % s.setting_dir
        s.db_url = '%s/db/ubinote.sqlite' % s.setting_dir 
        s.startup()

    def run(s):
        s.app.run()

    def startup(s):
        if not os.path.exists(s.setting_dir):
            setting.build_dir(s.setting_dir)
        if not os.path.exists(s.cfg_url):
            setting.build_cfg(s.cfg_url)
        if not os.path.exists(s.db_url):
            setting.build_db(s.db_url)
        else:
            setting.connect_db(s.db_url)
        setting.load(s.cfg_url)
        s.set_theme(setting.options['theme'])

    def set_theme(s, theme):
        setting.options['theme'] = theme
        path = '%s/themes/%s' % (s.setting_dir, theme)
        # use default theme if selected theme is invalid
        if not os.path.exists(path):
            s.templates_path = 'templates'
        else:
            s.templates_path = path
            print 'use Theme: %s' % theme
        s.render = render_mako(
            directories=[s.templates_path], 
            input_encoding='utf-8',
            output_encoding='utf-8',
            )

app = ubinote_app()
if __name__ == '__main__':
    sys.argv.append(setting.options['port'])
    app.run();

